const ajax = require('../../../utils/ajax/ajax.js')
const util = require('../../../utils/util.js')
Page({
    data: {
        bannerList: [],
        projectName: '',
        address: '',
        createTime: '',
        endTime: '',
        desContent: '',
        nowTimeStr: 0,
        endTimeStr: 0,
        proId: '',
        endDate: ''
    },
    onLoad: function (options) {
        const proId = options.proId
        this.getProjectDetail(proId)
    },
    getProjectDetail(proId, callback) {
        const that = this
        ajax.post({
            url: '/wx/getProjectDetail',
            data: {
                id: proId
            },
            success(res) {
                const data = res.data
                var nowTimeStr = new Date().getTime()
                var endTimeStr = data.end_time_date

                that.setData({
                    projectName: data.title,
                    address: data.address,
                    createTime: data.start_time,
                    endTime: data.end_time,
                    desContent: data.detail,
                    bannerList: data.pic_arr,
                    proId: proId,
                    nowTimeStr,
                    endTimeStr
                })
            },
            complete() {
                typeof (callback) === 'function' && callback()
            }
        })
    },

    btnApply() {
        const that = this
        const { projectName, address, proId } = that.data
        const firstImg = that.data.bannerList[0]

        ajax.post({
            url: '/wx/checkApplyQualification',
            data: {
                project_id: proId
            },
            success(res) {
                console.log(res);

                if (res.code === 200) {
                    wx.navigateTo({
                        url: `/pages/Index/listWriteInfo/listWriteInfo?projectImg=${firstImg}&projectName=${projectName}&address=${address}&proId=${proId}`,
                    })
                }
            },
            fail(err) {
                if (err.msg === '申请时间尚未开始') {
                    that.showTipPopup()
                    return
                }
                util.toast(err.msg)
            }
        })
    },
    comfirmCall() {
        this.showCallPopup()
        wx.makePhoneCall({
            phoneNumber: '13757189142'
        })
    },
    hiddleTipPopup() {
        this.showTipPopup()
    },
    callPopup() {
        this.showCallPopup()
    },
    showTipPopup() {
        const popup = this.selectComponent('#popupTip')
        popup.showPopup()
    },
    showCallPopup() {
        const popup = this.selectComponent('#popupCall')
        popup.showPopup()
    },
})